﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Printing;
using System.Windows.Shapes;
using ExhibitionDraw.Server;
using ExhibitionDraw.Service;

namespace ExhibitionDraw
{
    public partial class Index : UserControl
    {
        ObservableCollection<Service.FloorModel> floors;
        ObservableCollection<Service.PavilionModel> pavilions;
        ObservableCollection<Service.UserCompModel> cmpMdls;
        List<Service.PavilionModel> recpav = new List<Service.PavilionModel>();
        Service.CommServiceClient service = null;
        public Index()
        {
        
            InitializeComponent();
            InitControl();
            InitData();
        }
        /// <summary>
        /// 初始化页面控件
        /// </summary>
        private void InitControl()
        {
            OutPanel.Width = App.Current.Host.Content.ActualWidth;
            OutPanel.Height = App.Current.Host.Content.ActualHeight;
            LeftPanel.Width = 200;
            LeftPanel.Height = OutPanel.Height;
            RightPanel.Width = 200;
            RightPanel.Height = OutPanel.Height;
            CenterPanel.Width = OutPanel.Width - LeftPanel.Width - RightPanel.Width - 20;
            CenterPanel.Height = OutPanel.Height;
            CenterBottom.Margin = new Thickness(CenterPanel.Width - CenterBottom.Width, CenterPanel.Height - CenterBottom.Height * 2, 0, 0);
        }
        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void InitData()
        {
            //Service.CommServiceClient client = new Service.CommServiceClient();
            //var ServiceUrl = ServiceHelper.GetUrl() +"/WebService/CommService.svc";
            //client.Endpoint.Address = new System.ServiceModel.EndpointAddress(ServiceUrl);
            service = ServiceHelper.InitService();
            //获取楼层
            service.GetPavilionListAsync();
            service.GetPavilionListCompleted += new EventHandler<Service.GetPavilionListCompletedEventArgs>(GetPavilionListCompleted);
            //获取当前网站的推荐公司
            service.GetCurrentCompAsync(10,1);
            service.GetCurrentCompCompleted += new EventHandler<Service.GetCurrentCompCompletedEventArgs>(GetCompComplete);
        }

        /// <summary>
        /// 获取公司数据完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetCompComplete(object sender, Service.GetCurrentCompCompletedEventArgs e)
        {
            if (e != null)
            {

                if (e.Result.Count > 0)
                {
                    cmpMdls = (ObservableCollection<Service.UserCompModel>)e.Result;
                    listComp.ItemsSource = cmpMdls;
                }
            }
        }  
        /// <summary>
        /// 获取展馆完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetPavilionListCompleted(object sender, Service.GetPavilionListCompletedEventArgs e)
        {
            Service.PavilionModel pav = new Service.PavilionModel();
            pav.PaName = "选择展馆";
            recpav.Add(pav);
            if (e.Result.Count > 0)
            {
                pavilions = (ObservableCollection<Service.PavilionModel>)e.Result;
                recpav.AddRange(pavilions);
                cboxZhan.DisplayMemberPath = "PaName";
                cboxZhan.ItemsSource = recpav;
                cboxZhan.SelectedIndex = 0;
            }
            //Rectangle ret = new Rectangle();
            //ret.Width = 100;
            //ret.Height = 100;
            //ret.Fill = new SolidColorBrush(Colors.Red);

            //ret.SetValue(Canvas.LeftProperty,100);
            //ret.SetValue(Canvas.);
            //cvs.Children.Add(ret);
        }

        /// <summary>
        /// 点击打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            PrintDocument pdoc = new PrintDocument();
            pdoc.PrintPage += new EventHandler<PrintPageEventArgs>(document_PrintPage);
            pdoc.Print("Print Image");
        }

        /// <summary>
        /// 楼层选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboxFloor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FloorModel obj = ((ComboBox)sender).SelectedItem as FloorModel;
            if (obj != null)
            {
                try
                {
                    Img.Source = new BitmapImage(new Uri(Server.ServiceHelper.GetUrl() + obj.FloorPath));

                    // CreatMenu();
                }
                catch (Exception ex)
                {
                    //throw ex;
                }
            }
        }


        void document_PrintPage(object sender, PrintPageEventArgs e)
        {
            Image imagePrint = new Image();
            imagePrint.Source = Img.Source;
            imagePrint.Height = e.PrintableArea.Height;
            imagePrint.Width = e.PrintableArea.Width;
            e.PageVisual = imagePrint;
            e.HasMorePages = false;
        }

        /// <summary>
        /// 展馆选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboxZhan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Service.PavilionModel obj = ((ComboBox)sender).SelectedItem as Service.PavilionModel;
            var selID = Convert.ToInt32(obj.Id);
            service.GetFloorByPIdAsync(selID);
            service.GetFloorByPIdCompleted += new EventHandler<GetFloorByPIdCompletedEventArgs>(GetFloorByPIdCompleted);


        }

        /// <summary>
        /// 获取展馆楼层
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetFloorByPIdCompleted(object sender, GetFloorByPIdCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                floors = e.Result;
                if (floors.Count > 0)
                {
                    cboxFloor.DisplayMemberPath = "FloorName";
                    cboxFloor.ItemsSource = floors;
                    cboxFloor.SelectedIndex = 0;
                }
            }
        }

        private void silder_Change(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            //Img.Width //
            if (Img != null)
            {

                if ((e.OldValue - e.NewValue)==-1)
                {
                    Img.Width = Img.Width + 100;
                    Img.Height = Img.Height + 100;
                }
                if ((e.OldValue - e.NewValue)==1)
                {
                    Img.Width = Img.Width - 100;
                    Img.Height = Img.Height - 100;
                }
            }
        }
    }
}
